import typing
from typing import Optional

import PySide6.QtCore
from PySide6.QtGui import QIcon, QRegularExpressionValidator
import PySide6.QtWidgets
from PySide6.QtCore import Qt

from ..LittleWidgetBase import LittleWidgetBase
from .Ui_LWLineEdit import Ui_LWLineEdit

class LWLineEdit(LittleWidgetBase):
    def __init__(
        self, 
        parent: Optional[PySide6.QtWidgets.QWidget] = None, 
        f: PySide6.QtCore.Qt.WindowFlags = Qt.Widget) -> None:
        ################################################
        super().__init__(parent=parent, f=f)
        self._ui = Ui_LWLineEdit()
        self._ui.setupUi(self)
        self._propertys.update({
            'text': '',
            'regExp': '',
        })
        self._ui.lineEdit.editingFinished.connect(self._SlotLineEditTextChanged)

    def UpdateProperty(self, key: 'str', value: typing.Any) -> None:
        super().UpdateProperty(key, value)
        if (key not in self._propertys.keys()):
            return
        if (key == 'text'):
            if (not isinstance(value, str)):
                return
            self._ui.lineEdit.setText(value)
        if (key == 'regExp'):
            if (not isinstance(value, str)):
                return
            self._ui.lineEdit.setValidator(QRegularExpressionValidator(value))

    
    def _SlotLineEditTextChanged(self) -> None:
        '''
        function:
            当 LineEdit 中的文字变化的时候做什么
        '''
        self._UpdatePropertyValue({
            'text': self._ui.lineEdit.text(),
        })
        self.signalEvent.emit(self.EventTextChanged, self)